package com.dubbo.cp.consumer.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.cp.IService.BookService;
import com.dubbo.cp.entity.BookEntity;

@RestController
@RequestMapping("/book")
public class BookContrller extends BaseController{


	
	@Reference
	private BookService bookService;

	@GetMapping(value="/getBookList")
	@ResponseBody
	public List<BookEntity>getBookList(){
		return bookService.getBookList();

	}
	
	@GetMapping(value="/getBookEntityByBookName")
	@ResponseBody
	public BookEntity getBookEntityByBookName(@RequestParam("bookName") String bookName) {
		return bookService.getBookByBookName(bookName);
		
	}
	
	@GetMapping(value="/getCurrentBookList")
	@ResponseBody
	public List<BookEntity> getCurrentBookList(){
		return bookService.getCurrentBookList();
		
	}
	
	@GetMapping(value="/getPageBookList")
	@ResponseBody
	public Page<BookEntity> getPageBookList(){
		return bookService.getPageBookList();
		
	}
	
	@PutMapping(value="/addBookEntity")
	public BookEntity addBookEntity(BookEntity bookEntity) {
		return bookService.addBookEntity(bookEntity);
		
	}
	
	@PostMapping(value="/updateBookEntity")
	public BookEntity updateBookEntity(BookEntity bookEntity) {
		return bookService.updateBookEntity(bookEntity);
		
	}
	
	@PostMapping(value="/deleteBookEntity")
	public void deleteBookEntity(@RequestParam("id") Long id) {
		bookService.deleteBookEntityById(id);
	}
	
	protected void init(HttpServletRequest request,ServletRequestDataBinder binder) {
		SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dataFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dataFormat, false));
	}
}
